//#includes

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include <unistd.h>
#include "maestro.h"
#include "compass.h"
#include "gotemp.h"
#include "gps_boat.h"
#include <gps.h>

#define MAX_LINE 256

int rudder = -1;
int throttle = -1;
int stepper = -1;
int direction = -1;
int limitswitch = -1;
double rudderCoefficient = 0;
double throttleCoefficient = 0;
double rudderMixer = 1.0;
double throttleMixer = 1.0;
int boat = 0;
char boatIP[50] = "192.168.1.0";


int loadConfig()
{
	FILE *fp;
	fp = fopen("config.txt", "r");
	
	if (fp==NULL)
	{
		fprintf(stderr, "Error opening config file\n");
		exit(0);
	}
	
	fprintf(stderr, "Opened config file\n");

	
	char str[MAX_LINE];

	while (fgets(str, MAX_LINE, fp) != NULL)
	{
		//fprintf(stderr, "String: '%s' returned\n", str);
		char *pch;
		pch = strchr(str, '#');
		if (pch != NULL)
		{
			//fprintf(stderr, "Comment: %s", str);
		}
		else
		{
			int empty = strcmp(str, "\n");
			if (empty == 1)
			{
				char variable[50];
				char values[50];
				sscanf(str, "%s \t %[^\n] \n", variable, values);
				//fprintf(stderr, "Variable: %s \t values: %s\n", variable, values);
				if (!strcmp(variable, "rudder"))
				{
					sscanf(values, "[%d]", &rudder);
					fprintf(stderr, "Setting rudder to: %d\n", rudder);
				}
				else if(!strcmp(variable, "throttle"))
				{
					sscanf(values, "[%d]", &throttle);
					fprintf(stderr, "Setting throttle to: %d\n", throttle);
				}
				else if(!strcmp(variable, "stepper"))
				{
					sscanf(values, "[%d]", &stepper);
					fprintf(stderr, "Setting stepper to: %d\n", stepper);
				}
				else if(!strcmp(variable, "direction"))
				{
					sscanf(values, "[%d]", &direction);
					fprintf(stderr, "Setting direction to: %d\n", direction);
				}
				else if(!strcmp(variable, "limitswitch"))
				{
					sscanf(values, "[%d]", &limitswitch);
					fprintf(stderr, "Setting limit switch to: %d\n", limitswitch);
				}
				else if(!strcmp(variable, "rudderCoefficient"))
				{
					sscanf(values, "[%lf]", &rudderCoefficient);
					fprintf(stderr, "Setting rudderCoeff to: %lf\n", rudderCoefficient);
				}
				else if(!strcmp(variable, "throttleCoefficient"))
				{
					sscanf(values, "[%lf]", &throttleCoefficient);
					fprintf(stderr, "Setting throttleCoeff to: %lf\n", throttleCoefficient);
				}
				else if(!strcmp(variable, "rudderMixer"))
				{
					sscanf(values, "[%lf]", &rudderMixer);
					fprintf(stderr, "Setting rudder mixer to: %lf\n", rudderMixer);
				}
				else if(!strcmp(variable, "throttleMixer"))
				{
					sscanf(values, "[%lf]", &throttleMixer);
					fprintf(stderr, "Setting throttle to: %lf\n", throttleMixer);
				}
				else if(!strcmp(variable, "boatNumber"))
				{
					sscanf(values, "[%d]", &boat);
					fprintf(stderr, "Setting boat number to: %d\n", boat);
				}
				else if(!strcmp(variable, "boatIP"))
				{
					sscanf(values, "[%s]", &boatIP);
					fprintf(stderr, "Boat IP is: %s\n", boatIP);
				}		
				else
				{
					fprintf(stderr, "cannot parse %s\n", variable);
				}
			}
			else
			{
				//fprintf(stderr, "Empty line\n");
			}
		}
	}

	fclose(fp);
	fprintf(stderr, "Finished reading config file. \n");
	/***********************************************/
	return 0;
}


int getRudder()
{
	return rudder;
}

int getThrottle()
{
	return throttle;
}

int getStepper()
{
    return stepper;
}

int getDirection()
{
    return direction;
}

int getLimitswitch()
{
    return limitswitch;
}

double getRudderCoeff()
{
	return rudderCoefficient;
}
double getThrottleCoeff()
{
	return throttleCoefficient;
}

double getRudderMixer()
{
	return rudderMixer;
}
double getThrottleMixer()
{
	return throttleMixer;
}
double getBoat()
{
	return boat;
}
